package com.lanou.service.impl;

import com.lanou.bean.LayUIGridResultInfo;
import com.lanou.bean.User;
import com.lanou.mapper.UserMapper;
import com.lanou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private List<User> userCache;
    @Autowired
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User oneServler(String username, String password) {
        return null;
    }


    @Override
    public int update(Integer id) {
        return userMapper.updateUser(id);
    }

    @Override
    public int addUser(String userName, String password, String phoneNumber, String email, String roleName) {
        Integer ur = null;
        if (roleName.equals("超级管理员")){
            ur = 1;
        }else if (roleName.equals("总编")){
            ur=2;
        }else {
            ur=5;
        }

        int i = userMapper.insert(new User(userName, password, Integer.parseInt(phoneNumber), email, ur, new Date(), 1, null));
        return i;
    }

    @Override
    public LayUIGridResultInfo<User> queryAdminsForLayUI(Integer pageSize, Integer currentPage) {
        LayUIGridResultInfo<User> layUIGridResultInfo = new LayUIGridResultInfo<>();

        userCache = userMapper.selectAllAdmins();

        int startIdx = (currentPage - 1) * pageSize;
        int endIdx = currentPage * pageSize;
        if(endIdx > userCache.size()) {
            endIdx = userCache.size();
        }

        List<User> currentPageData = userCache.subList(startIdx, endIdx);
        layUIGridResultInfo.setCount((long) userCache.size());
        layUIGridResultInfo.setCode(0);
        layUIGridResultInfo.setData(currentPageData);

        return layUIGridResultInfo;
    }

}

